using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;

public partial class Admin_NhaSanXuatDanhSach : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
            loadDropDownList();
            loadData();
        }
    }
    private void loadDropDownList()
    {
        tbNhaSXCollection nsxCollection = tbNhaSXBUS.GetAll();
        ddl_mansx.DataValueField = tbNhaSXColumns.MaNSX.ToString();
        ddl_mansx.DataSource = nsxCollection;
        ddl_mansx.DataBind();
        ddl_mansx.Items.Insert(0, new ListItem("All", "0"));
        
    }
    private void loadData()
    {
        Pager.PageSize = 5;
        Criteria c = new Criteria();
        if (txt_tennsx.Text != "")
        {
            c.add(Condition.AND, tbNhaSXColumns.TenNSX, Condition.LIKE,
                txt_tennsx.Text);
        }
        if (ddl_mansx.SelectedValue != "0")
        {
            c.add(Condition.AND, tbNhaSXColumns.MaNSX, Condition.EQUAL, ddl_mansx.SelectedValue);
        }

        int total = 0;
        tbNhaSXCollection nhasxCollection =
        tbNhaSXBUS.Get(c.Criter, Pager.CurrentIndex, Pager.PageSize,
        tbNhaSXColumns.MaNSX.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = nhasxCollection;
        Repeater1.DataBind();
        Pager.ItemCount = total;
        if (total <= Pager.PageSize)
        {
            Pager.Visible = false;
        }
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem ||
               e.Item.ItemType == ListItemType.Item)
            {
                tbNhaSX nhasx = (tbNhaSX)e.Item.DataItem;
                //Literal lit_MaNSX = (Literal)e.Item.FindControl("lit_MaNSX");
                Literal lit_TenNSX = (Literal)e.Item.FindControl("lit_TenNSX");
                Literal lit_DiaChi = (Literal)e.Item.FindControl("lit_DiaChi");
                Literal lit_DienThoai = (Literal)e.Item.FindControl("lit_DienThoai");
                HyperLink hpl_Edit = (HyperLink)e.Item.FindControl("hpl_Edit");
                hpl_Edit.NavigateUrl = "~/Admin/NhaSanXuatUpdate.aspx?id=" + nhasx.MaNSX.ToString();
            }
        }

    protected void Pager_Command(object sender, CommandEventArgs e)
    {
        Pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        loadData();
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        loadData();
    }
    
    
    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_Xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
            if (cb_Xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int iD = int.Parse(hf_id.Value);
                tbNhaSX nhasx= tbNhaSXBUS.GetByID(iD);
                tbNhaSXBUS.Delete(iD);
            }
        }
        Response.Redirect(Request.RawUrl);
    }
    
}